package com.jeeplus.modules.common.excel.core;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.function.Consumer;

/**
 * @author wang.yishu
 * @version 1.0
 * @className OnlyReadHeadListener
 * @description TODO
 * @date 2022/8/12 20:22
 */
public class OnlyReadHeadListener<T> implements ReadListener<T> {
    public static int BATCH_COUNT = 100;
    private List<T> cachedDataList;
    private final Consumer<List<T>> consumer;

    private Map<Integer, String> cellTitleMap = new HashMap<>();

    private Set<String> titleSet = new HashSet<>();

    public OnlyReadHeadListener(Consumer<List<T>> consumer) {
        this.cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        this.consumer = consumer;
    }

    @Override
    public void invoke(T data, AnalysisContext context) {
        this.cachedDataList.add(data);
        if (this.cachedDataList.size() >= BATCH_COUNT) {
            this.consumer.accept(this.cachedDataList);
            this.cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
        Set<Integer> keys = headMap.keySet();
        for (Integer key : keys) {
            ReadCellData<?> readCellData = headMap.get(key);
        }//
        Map<Integer, Cell> cellMap = context.readRowHolder().getCellMap();
        for (Integer integer : cellMap.keySet()) {
            Cell cell = cellMap.get(integer);
            if (cell instanceof ReadCellData) {
                String stringValue = ((ReadCellData) cellMap.get(integer)).getStringValue();
                this.cellTitleMap.put(integer, stringValue);
                this.titleSet.add(stringValue);
            }
        }
        Map<Integer, Head> head = context.currentReadHolder().excelReadHeadProperty().getHeadMap();
        Map<String,String> classMap = new HashMap<>();
        for(Object map:head.entrySet()){
            Head h = (Head) ((Map.Entry)map).getValue();
            classMap.put(h.getFieldName(),h.getHeadNameList().get(0));
        }
        this.cachedDataList.add((T) classMap);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (CollectionUtils.isNotEmpty(this.cachedDataList)) {
            this.consumer.accept(this.cachedDataList);
        }

    }

    public Map<Integer, String> getCellTitleMap() {
        return cellTitleMap;
    }

    public void setCellTitleMap(Map<Integer, String> cellTitleMap) {
        this.cellTitleMap = cellTitleMap;
    }

    public Set<String> getTitleSet() {
        return titleSet;
    }

    public void setTitleSet(Set<String> titleSet) {
        this.titleSet = titleSet;
    }
}
